#!/usr/bin/env python
# coding: utf-8

# In[5]:


"""随机森林模型"""

import pandas as pd
from sklearn.ensemble import RandomForestClassifier

# 读取数据
train = pd.read_csv("D:\\data\\1\\train.csv")
test = pd.read_csv("D:\\data\\1\\test.csv")
submit = pd.read_csv("D:\\data\\1\\sample_submit.csv")

# 删除id
train.drop('CaseId', axis=1, inplace=True)
test.drop('CaseId', axis=1, inplace=True)

# 取出训练集的y
y_train = train.pop('Evaluation')

# 建立随机森林模型
clf = RandomForestClassifier(n_estimators=100, random_state=0)
clf.fit(train, y_train)
y_pred = clf.predict_proba(test)[:, 1]

# 输出预测结果至my_RF_prediction.csv
submitData = pd.read_csv(r'D:\\data\\1\\my_RF_prediction.csv')
submit['Evaluation'] = y_pred
submit.to_csv('my_RF_prediction.csv', index=False)


# In[2]:


"""检测训练精确度"""

# freature importances
print(clf.feature_importances_)

# Train accuracy
from sklearn.metrics import accuracy_score
y_train_pred = clf.predict(train)
print(y_train_pred)

acc_train = accuracy_score(y_train, y_train_pred)
print("acc_train = %f" % (acc_train))


# In[5]:


"""模型参数改进"""
"""数据预处理"""
import numpy as np

# 缺失值
print(train.info()) # 打印检查是否有缺失值，返回None则无缺失值

# 观察变量信息
print(train.describe())

# 查看相关系数
corr = train.corr()
corr[np.abs(corr) < 0.2] = np.nan #绝对值低于0.2的就用nan替代
print(corr)


# In[2]:


"""选择criterion参数"""

RF = RandomForestClassifier(random_state = 66)
score = cross_val_score(RF,x,y,cv=10).mean()
print('基尼系数得分: %.4f'%score)
RF = RandomForestClassifier(criterion = 'entropy',random_state = 66)
score = cross_val_score(RF,x,y,cv=10).mean()
print('熵得分: %.4f'%score)


# In[3]:


"""选择n_estimators的最佳值"""

###调n_estimators参数
ScoreAll = []
for i in range(10,200,10):
    DT = RandomForestClassifier(n_estimators = i,random_state = 66) #,criterion = 'entropy'
    score = cross_val_score(DT,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[5]:


"""缩小n_estimators最优值范围"""

###进一步缩小范围，调n_estimators参数
ScoreAll = []
for i in range(120,160):
    DT = RandomForestClassifier(n_estimators = i,random_state = 66)   #criterion = 'entropy',
    score = cross_val_score(DT,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[6]:


"""选择max_depth最优值"""

###粗调max_depth参数
ScoreAll = []
for i in range(10,30,3):
    DT = RandomForestClassifier(n_estimators = 151,random_state = 66,max_depth =i ) #,criterion = 'entropy'
    score = cross_val_score(DT,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[8]:


"""缩小max_depth最优值的范围和步长"""


ScoreAll = []
for i in range(15,20,1):
    DT = RandomForestClassifier(n_estimators = 151,random_state = 66,max_depth =i ) #,criterion = 'entropy'
    score = cross_val_score(DT,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[9]:


"""选择min_samples_split的最佳参数"""

###调min_samples_split参数
ScoreAll = []
for i in range(2,10):
    RF = RandomForestClassifier(n_estimators = 151,random_state = 66,max_depth =17,min_samples_split = i ) #,criterion = 'entropy'
    score = cross_val_score(RF,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[10]:


"""选择min_samples_leaf的最佳参数"""

###调min_samples_leaf参数
ScoreAll = []
for i in range(1,15,2):
    DT = RandomForestClassifier(n_estimators = 151,random_state = 66,max_depth =17,min_samples_leaf = i,min_samples_split = 2 ) 
    score = cross_val_score(DT,data.data,data.target,cv=10).mean()
    ScoreAll.append([i,score])
ScoreAll = np.array(ScoreAll)

max_score = np.where(ScoreAll==np.max(ScoreAll[:,1]))[0][0] ##这句话看似很长的，其实就是找出最高得分对应的索引
print("最优参数以及最高得分:",ScoreAll[max_score])  
plt.figure(figsize=[20,5])
plt.plot(ScoreAll[:,0],ScoreAll[:,1])
plt.show()


# In[11]:


"""选择max_features的最佳参数"""

#调max_features参数
param_grid = {
    'max_features':np.arange(0.1, 1)}

rfc = RandomForestClassifier(random_state=66,n_estimators = 151,max_depth = 17,min_samples_leaf =1 ,min_samples_split =2 )
GS = GridSearchCV(rfc,param_grid,cv=10)
GS.fit(data.data,data.target)
print(GS.best_params_)
print(GS.best_score_)


# In[3]:


"""随机森林的最终结果"""

#导入必要的包
from sklearn.ensemble import RandomForestClassifier 
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split,GridSearchCV,cross_val_score
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

# 读取数据
train = pd.read_csv("D:\\data\\1\\train.csv")
test = pd.read_csv("D:\\data\\1\\test.csv")
submit = pd.read_csv("D:\\data\\1\\sample_submit.csv")

#导入数据集
data = load_digits()
x = data.data
y = data.target

# 删除id
train.drop('CaseId', axis=1, inplace=True)
test.drop('CaseId', axis=1, inplace=True)

# 取出训练集的y
y_train = train.pop('Evaluation')

# 建立随机森林模型
clf = RandomForestClassifier(criterion = 'entropy',random_state=66,n_estimators = 151,max_depth = 17,min_samples_leaf =1 ,min_samples_split =2 )
clf.fit(train, y_train)
y_pred = clf.predict_proba(test)[:, 1]

# 输出预测结果至my_RF_prediction.csv
submit['Evaluation'] = y_pred
submit.to_csv('my_RF_prediction2.csv', index=False)


# In[4]:


"""检测训练精确度"""

# freature importances
print(clf.feature_importances_)

# Train accuracy
from sklearn.metrics import accuracy_score
y_train_pred = clf.predict(train)
print(y_train_pred)

acc_train = accuracy_score(y_train, y_train_pred)
print("acc_train = %f" % (acc_train))


# In[ ]:




